var page_fullscreen = {
	tr: null,
	currentItem:0,
    effect: null,
    fullscreen : null,
    caches : {},
    init: function() {
	    // 处理全屏效果
	    page_fullscreen.fullScreen = $('#fullScreen').fullScreen();
	
	    $(page_fullscreen.fullScreen).bind('fullscreenShow', function() {
          $(window).bind('resize', {scope: this}, function(evt) {
              evt.data.scope.doLayout();
           })
          .bind('scroll',  {scope: this}, function(evt) {
              evt.data.scope.doLayout();
          });
          this.gallery.find('#btnExit').bind('click', {scope: this}, function(evt) {
              evt.data.scope.hide();
          });
          if (page_fullscreen.caches.loaded == undefined) {
              this.gallery.find('#gallery').css({
                'text-align': 'center'
              });
              this.gallery.height('100%');
              page_fullscreen.caches.galleryWidth = this.gallery.width();
			  page_fullscreen.caches.loaded = 'true';
		  }
    });
	
	$(page_fullscreen.fullScreen).bind('fullscreenApplyLayout', function() {
        var _currentItem = page_fullscreen.effect.getCurrentItem(); 
        // 处理中间图片居中
        this.gallery.width((page_fullscreen.caches.galleryWidth > this.container.width()) ? this.container.width() : page_fullscreen.caches.galleryWidth);
        this.gallery.find('#gallery .photo-b').width(this.gallery.width());

        // 自动调整navibar的宽度 
		$("#fullScreen .photo-page2-2").css('width', this.gallery.width() - 178);
		$("#fullScreen .photo-page2-2").parent().css('width', this.gallery.width()); 
        

        var _maxHeight = 0;
        this.gallery.find('.photo-b').each(function(_idx) {
          (function(_obj) {
              var _fullScreen = page_fullscreen.fullScreen;
              var _height = _fullScreen.container.height() - page_fullscreen.caches.heightPadding;
              if (_height <= 10) { _height = 10; }
              if (_height > 460) { _height = 460; }
              if ( (_height * page_fullscreen.caches.imgScale[_idx] + 20) >= _fullScreen.gallery.width() ) {
                _height = (_fullScreen.gallery.width() - 20)/page_fullscreen.caches.imgScale[_idx];
              }
              if (_maxHeight < _height) {
                _maxHeight = _height;
              }
              _obj.height(_height);
          })($('> img', $(this)));
        });
       
        // 调整最大高度，防止屏幕因为图片切换不同高度而抖动
        this.gallery.find('#gallery .photo-b').height(_maxHeight); 
        this.gallery.find('#gallery').width(_currentItem.width()).height(_currentItem.height()).end()
                    .find('.photo-b img').each(function() {
                          $(this).css({
                            'margin-top'      : ($(this).parent().height() - $(this).height())/2,
                            'margin-bottom'   : ($(this).parent().height() - $(this).height())/2,
                            'margin-left'     : 'auto',
                            'margin-right'    : 'auto'
                          });
                     });
	});

    // 处理图片轮换效果
    page_fullscreen.effect = page_fullscreen.fullScreen.gallery.find('#gallery').fadeAffect({pauseTime: 700000000, transitionTime: 1500});
    page_fullscreen.effect.stop();
  },

  updateLayout : function() {
     var _scale = new Array();
     page_fullscreen.fullScreen.gallery.find('.photo-b img').each(function(_idx) {
        var _img = {'width' : $(this)[0].width, 'height' : $(this)[0].height};
        _scale[_idx] = _img.width / _img.height;
    });
    page_fullscreen.caches.imgScale = _scale;
    page_fullscreen.caches.heightPadding = 100;
  },

  start: function() {
     this.updateLayout();
     page_fullscreen.fullScreen.show();
     var navWidth = page_fullscreen.fullScreen.gallery.find('.photo-page2-2-bg').outerWidth(true) * page_fullscreen.fullScreen.gallery.find('.photo-page2-2-bg').length;
     page_fullscreen.fullScreen.gallery.find('.photo-page2-2-list a').each(function(_idx){
        $(this).bind('click', function() {
            page_fullscreen.showItem(_idx);
            page_fullscreen.effect.stop();
            page_fullscreen.fullScreen.doLayout();
        });
     });
     page_fullscreen.fullScreen.gallery.find('a.navicontroller_prev').click(function() {
        var _currentIdx = page_fullscreen.effect.getCurrentIndex(); 
        if (_currentIdx == 0) {
            page_fullscreen.effect.showItem(_currentIdx);
            page_fullscreen.effect.stop();
            page_fullscreen.fullScreen.doLayout();
        }else{
            page_fullscreen.effect.showItem(_currentIdx - 1);
            page_fullscreen.effect.stop();
            page_fullscreen.fullScreen.doLayout();
        }

        var _width = page_fullscreen.fullScreen.gallery.find('.photo-page2-2-bg').eq(_currentIdx).outerWidth(true); 
        var _strMarginLeft = page_fullscreen.fullScreen.gallery.find('.navicontroller').css('margin-left');
        var _marginLeft = parseInt(_strMarginLeft.substring(0, _strMarginLeft.indexOf('px')));
        if (_marginLeft + _width <= 0) {
            page_fullscreen.fullScreen.gallery.find('.navicontroller').css('margin-left', _marginLeft + _width); 
        }
     });
     page_fullscreen.fullScreen.gallery.find('a.navicontroller_next').click(function() {
        var _currentIdx = page_fullscreen.effect.getCurrentIndex(); 
        if (_currentIdx == page_fullscreen.fullScreen.gallery.find('.photo-page2-2-bg').length - 1) {
            page_fullscreen.effect.showItem(_currentIdx);
            page_fullscreen.effect.stop();
            page_fullscreen.fullScreen.doLayout();
            return false;
        }
        page_fullscreen.effect.showItem(_currentIdx + 1);
        page_fullscreen.effect.stop();
        page_fullscreen.fullScreen.doLayout();
        var _width = page_fullscreen.fullScreen.gallery.find('.photo-page2-2-bg').eq(_currentIdx).outerWidth(true); 
        var _strMarginLeft = page_fullscreen.fullScreen.gallery.find('.navicontroller').css('margin-left');
        var _marginLeft = parseInt(_strMarginLeft.substring(0, _strMarginLeft.indexOf('px')));
        var _strMaxWidth = page_fullscreen.fullScreen.gallery.find('.photo-page2-2').css('width');
        var _maxWidth = parseInt(_strMaxWidth.substring(0, _strMaxWidth.indexOf('px')));
        if ( navWidth - _width + _marginLeft > _maxWidth) {
           page_fullscreen.fullScreen.gallery.find('.navicontroller').css('margin-left', _marginLeft - _width); 
        }
     });
  },
  end: function() {
     page_fullscreen.fullScreen.hide();
     page_fullscreen.effect.stop();
     page_fullscreen.fullScreen.gallery.find('.photo-page2-2-list a').each(function(_idx){
         $(this).unbind('click');
     });

     page_fullscreen.fullScreen.gallery.find('a.navicontroller_prev').unbind('click');
     page_fullscreen.fullScreen.gallery.find('a.navicontroller_prev').unbind('click');
  },

  showItem: function(_idx) {
     page_fullscreen.effect.showItem(_idx);
     page_fullscreen.effect.stop();
     this.updateLayout();
  }

};

(function($){
    $.fn.extend({
        center: function () {
            return this.each(function() {
                var top = ($(window).height() - $(this).outerHeight()) / 2;
                var left = ($(window).width() - $(this).outerWidth()) / 2;
                $(this).css({position:'absolute', margin:0, top: (top > 0 ? top : 0)+'px', left: (left > 0 ? left : 0)+'px'});
            });
        }
    }); 
   $.fn.extend({
        left: function () {
            return this.each(function() {
                var top = ($(window).height() - $(this).outerHeight()) / 2;
                var left = 0;
                $(this).css({position:'absolute', margin:0, top: (top > 0 ? top : 0)+'px', left: (left > 0 ? left : 0)+'px'});
            });
        }
    });  
    $.fn.extend({
        right: function () {
            return this.each(function() {
                var top = ($(window).height() - $(this).outerHeight()) / 2;
                var left = $(window).width() - $(this).outerWidth();
                $(this).css({position:'absolute', margin:0, top: (top > 0 ? top : 0)+'px', left: (left > 0 ? left : 0)+'px'});
            });
        }
    }); 
    
    $.fn.extend({
        bottom: function () {
            return this.each(function() {
                var top = $(window).height() - $(this).outerHeight();
               // var left = $(window).width() - $(this).outerWidth();
                $(this).css({position:'absolute', margin:0, top: (top > 0 ? top : 0)+'px'});
            });
        }
    }); 
})(jQuery);
